package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfUser;
import cn.org.timespace.easyframe.model.permission.EfUserLoginLog;
import cn.org.timespace.easyframe.service.permission.EfUserLoginLogService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.easyframe.util.pager.Pager;
import cn.org.timespace.easyframe.util.pager.PagerHelper;

/**
 * <p>
 * 用户登录日志
 * </p>
 * 
 * @version 1.0, 11/02/11
 * @since 1.0
 * @author wubo
 */

public class EfUserLoginLogAction extends EFBaseAction {

	private static final long serialVersionUID = -3224275471309009166L;
	private static Log log = LogFactory.getLog(EfUserLoginLogAction.class);
	
	private EfUserLoginLogService efUserLoginLogService;
	
	// 该构造函数必须这样实现
	public EfUserLoginLogAction() {
		super(new EfUserLoginLogModel());
	}
	
	/**
	 * 查询用户登录日志
	 * @return
	 * @throws Exception
	 */
	public String doList() throws Exception {
		
		EfUserLoginLogModel loginLogModel = getEfUserLoginLogModel();
		
		Pager p = PagerHelper.getPagerInstance(getRequest());
		
		List<Object> resList = 
			efUserLoginLogService.findByCondition(loginLogModel,p);
		
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		
		return LIST;

	}
	
	/**
	 * 保存用户登录日志
	 * @return
	 * @throws Exception
	 */
	public String doSave() throws Exception {
		
		EfUserLoginLogModel loginLogModel = (EfUserLoginLogModel) this.getModel();
		
		//获取主键
		String logId = IdentityGenerator.getInstance().getId();
		//构造用户对象
		EfUser efUser = new EfUser();
		efUser.setUserId(getUserId());
		
		EfUserLoginLog loginLog = loginLogModel.getEfUserLoginLog();
		loginLog.setEfUser(efUser);
		loginLog.setLogId(logId);
		loginLog.setLogonDatetime(getCurDate());
		
		efUserLoginLogService.save(loginLog);
		
		return doList();
		
	}
	
	/**
	 * 修改用户登录日志
	 * @return
	 * @throws Exception
	 */
	public String doUpdate() throws Exception {
		
		EfUserLoginLog loginLog = getEfUserLoginLogModel().getEfUserLoginLog();
		efUserLoginLogService.update(loginLog);
		return doList();
		
	}
	
	/**
	 * 根据主键查询用户日志ID
	 * @return
	 * @throws Exception
	 */
	public String doFindById() throws Exception {
		
		String logId = getEfUserLoginLogModel().getLogId();
		
		if(!StringUtils.isEmpty(logId)){  //更新
			List<EfUserLoginLog> resList = efUserLoginLogService.findByCondition(getEfUserLoginLogModel());
			if(resList != null && resList.size() == 1){
				getEfUserLoginLogModel().setEfUserLoginLog(resList.get(0));
			}
			return UPDATE;
		}
		
		return ADD;
	}

	public EfUserLoginLogService getEfUserLoginLogService() {
		return efUserLoginLogService;
	}

	public void setEfUserLoginLogService(EfUserLoginLogService efUserLoginLogService) {
		this.efUserLoginLogService = efUserLoginLogService;
	}
	
	private EfUserLoginLogModel getEfUserLoginLogModel(){
		return (EfUserLoginLogModel) this.getModel();
	}
}
